<?php
namespace Home\Controller;
use Think\Controller;
use Think\Model;
class IndexController extends HomeController {
    //首页
    public function index(){
        $lunboDb=D('poster');
        $where['status']=1;
        $lunbolists=$lunboDb->where(array('status'=>1,'mold'=>1))->limit(4)->select();
        $hotsales=D('poster_hotsale')->where($where)->limit(4)->select();
        //读取分类列表
        $labelTable=D('config');
        $where['name']='LABEL_SET';
        $labellist=$labelTable->where($where)->getfield('value');
        $str= explode("\n", $labellist);
        $arr=array();
        foreach ($str as $value){
            $arr2=explode(":", $value);
            if($arr2[0]==""||$arr2[0]==" "||$arr2[0]==null){
            }else{
                $arr[$arr2[0]]=$arr2[1];
            }
        }
        $goodslists=$this->get_data_list(array('TJ'),$arr);
        $this->assign('list',$lunbolists);
        $this->assign('hotsales',$hotsales);
        $this->goodslists=$goodslists;
        $this->display();
        }
    function get_data_list($arr,$arr2){
            $goodslist= array();
            for($i=0;$i<count($arr);$i++){
                $goodslist[$i]['name']=$arr2[$arr[$i]];
                $goodslist[$i]['url']="{:U('Product/ProductList')}";
                $goodslist[$i]['good']=$this->get_data($arr[$i]);
            }
            return $goodslist;
        }
    function get_data($keyword){
            $sql="select a.*,(CASE WHEN b.price2 <> 0 THEN b.price2 WHEN (least(a.price,b.price1)) is NOT NULL THEN b.price1  ELSE a.price END ) as minprice  from gr_product a LEFT JOIN gr_activity_more_buy b ON (a.id=b.id)  where a.status=1 AND a.inventory >0 AND a.tagarray like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc limit 100";
//            $sql="select a.* ,a.price as minprice from gr_product a  where a.status=1 AND a.inventory >0 AND a.tagarray like '%".$keyword."%' order by a.recommend desc,a.inventory desc,a.createdate desc limit 4";
            $model=new Model();
            $goodsres=$model->query($sql);
            $i=0;
            foreach($goodsres as $value){
                preg_match_all('/\["(.+?)\"(.+?)/', $value["picurlarray"], $arr4);
                $goodsres[$i]['imgurl']=$arr4[1][0];
//                $isCart=R("Activity/isCart",array($goodsres[$i]['id']));
                $isCart=$this->isCart($goodsres[$i]['id']);
                $goodsres[$i]['iscart']=$isCart;
                $i++;
            }
             return $goodsres;
           }
    public function cart() {
        if (session('userphone')) {
            $ConfigRes=M('Config')->where(array('name'=>'NO_LIMIT_BUYER'))->getField('value');
            $specialArr=explode("\n",$ConfigRes);
            $userid = session('userid');
            foreach($specialArr as $k=>$v){
                $specialArr[$k]=intval(trim($v),10);
            }
            $inituserid=intval($userid,10);
            if(in_array($inituserid,$specialArr)){
                $this->special=1;
            }else{
                $this->special=0;
            }
            $map2['id']=$userid;
            $map["status"]=2;
            $map["uid"]=$userid;
            $cartTable= D('cart');
            $cartTable->where($map)->delete();
            $orderPrice = 0;
            $cartPro = D('product');
            $sqlpro = "select a.pid,a.pnum,b.name,b.quantity,b.picurlarray,b.price from gr_cart a,gr_product b where"
                    . " a.uid = $userid and a.pid = b.id and a.status = 1";
            $cartPros = $cartPro->query($sqlpro);
            $activityMoreBuy=M('activity_more_buy');
            for ($i = 0; $i < count($cartPros); $i++) {
                $isActivity=$activityMoreBuy->where('id='.$cartPros[$i]['pid'])->select();
                if(count($isActivity)>0){
                    $oldprice =  $cartPros[$i]['price'];
                    $price1 = $isActivity[0]['price1'];
                    $price2 = $isActivity[0]['price2'];
                    $num1 = $isActivity[0]['num1'];
                    $num2 = $isActivity[0]['num2'];
                    $cartPros[$i]['num0'] = "1-".($num1-1)."件:";//展示用
                    if($num1 == 2){
                        $cartPros[$i]['num0'] = "1"."件:";
                    }
                    $cartPros[$i]['num1'] = "≥".$num1."件:";//展示用
                    $cartPros[$i]['price0'] ="￥".$oldprice;
                    $cartPros[$i]['price1'] = "￥".$price1;
                    if($price2>0 && $num2>0){
                        $cartPros[$i]['num2']="≥".$num2."件:";//展示用
                        $cartPros[$i]['price2']="￥".$price2;
                    }else{
                        $cartPros[$i]['num2']="";//展示用
                        $cartPros[$i]['price2']="";
                    }
                    if($cartPros[$i]['pnum']>=$num1 && $num1>0 && $price1>0){
                        $cartPros[$i]['price']=$price1;
                        if($cartPros[$i]['pnum'] >= $num2 && $num2 > 0 &&$price2>0){
                            $cartPros[$i]['price']=$price2;
                        }
                    }
                }else{
                    $cartPros[$i]['num0'] = "暂无优惠";//展示用
                    $cartPros[$i]['num1'] = "";//展示用
                    $cartPros[$i]['price0'] = "";
                    $cartPros[$i]['price1'] = "";
                    $cartPros[$i]['num2']="";//展示用
                    $cartPros[$i]['price2']="";
                }
                $picarray = json_decode($cartPros[$i]['picurlarray']);
                $cartPros[$i]['pic'] = $picarray[0];
                $cartPros[$i]['sumproprice'] = $cartPros[$i]['pnum'] * $cartPros[$i]['price'];
                $orderPrice += $cartPros[$i]['pnum'] * $cartPros[$i]['price'];
            }
            $requriePrice=$orderPrice;
            $this->assign("orderPrice", $orderPrice);
            $this->assign("requriePrice", $requriePrice);
            $this->assign('cartPros', $cartPros);
            $this->assign("proCount",count($cartPros));
            $cartAddress = D('addr');
            $sqladdress = "select * from gr_addr  where uid = $userid and status = 1";
            $cartAddresses = $cartAddress->query($sqladdress);
            $buyers = M("buyers")->where($map2)->find();
            $this->buyers = $buyers;
            $this->buynow = 0;
            $this->assign('cartAddresses', $cartAddresses);
            $this->display();
        } else {
            $this->redirect('User/edit');
        }
    }
    function isCart($pid){
        $data['pid']=$pid;
        $data['uid']=session('userid');
        $flagres=D('cart')->where($data)->select();
        if(count($flagres)==0){
            $iscartflag=false;
        }else{
            $iscartflag=true;
        }
        return $iscartflag;
    }

}
